package com.love.marriage.TecentIm.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.love.marriage.TecentIm.service.WuKongImService;
import com.love.marriage.TecentIm.vo.LocalImChatListVo;
import com.love.marriage.common.vo.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * WuKongIM 控制器
 * 提供与 WuKongIM 服务相关的 REST API 接口
 */
@ApiSort(110)
@Tag(name = "WuKongIM 聊天", description = "WuKongIM 聊天相关接口")
@RestController
@RequiredArgsConstructor
@RequestMapping("/wukongim")
public class WuKongImController {

    private final WuKongImService wuKongImService;

    @ApiOperationSupport(order = 17, author = "Qwen")
    @Operation(summary = "获取IM聊天记录", description = "根据条件获取频道聊天记录（消息）")
    @ApiResponse(responseCode = "200", description = "Success",
            content = @Content(mediaType = "application/json",
                    schema = @Schema(implementation = Result.class)))
    @Parameters({
            @Parameter(name = "loginUid", description = "当前登录用户ID", required = true),
            @Parameter(name = "channelId", description = "频道ID", required = true),
            @Parameter(name = "channelType", description = "频道类型（1: 个人频道，2: 群组频道）", required = false),
            @Parameter(name = "startMessageSeq", description = "开始消息序号", required = false),
            @Parameter(name = "endMessageSeq", description = "结束消息序号", required = false),
            @Parameter(name = "limit", description = "限制数量", required = false),
            @Parameter(name = "pullMode", description = "拉取模式（0: 向下拉取, 1: 向上拉取）", required = false)
    })
    @PostMapping("/getImChatHistory")
    public Result<List<Map<String, Object>>> getImChatHistory(@RequestParam String loginUid,
                                           @RequestParam String channelId,
                                           @RequestParam(required = false) Integer channelType,
                                           @RequestParam(required = false) Long startMessageSeq,
                                           @RequestParam(required = false) Long endMessageSeq,
                                           @RequestParam(required = false) Integer limit) {
        List<Map<String, Object>> result = wuKongImService.getImChatHistory(loginUid, channelId, channelType, startMessageSeq, endMessageSeq, limit);
        return Result.success(result);
    }

    @ApiOperationSupport(order = 18, author = "Qwen")
    @Operation(summary = "设置会话未读数", description = "设置会话未读数")
    @ApiResponse(responseCode = "200", description = "Success",
            content = @Content(mediaType = "application/json",
                    schema = @Schema(implementation = Result.class)))
    @Parameters({
            @Parameter(name = "uid", description = "用户ID", required = true),
            @Parameter(name = "channelId", description = "频道ID", required = true),
            @Parameter(name = "channelType", description = "频道类型", required = true),
            @Parameter(name = "unreadCount", description = "未读数量", required = true)
    })
    @PostMapping("/setConversationUnread")
    public Result<Object> setConversationUnread(@RequestParam String uid,
                                               @RequestParam String channelId,
                                               @RequestParam Integer channelType,
                                               @RequestParam Integer unreadCount) {
        Object result = wuKongImService.setConversationUnread(uid, channelId, channelType, unreadCount);
        return Result.success(result);
    }

    @ApiOperationSupport(order = 20, author = "Qwen")
    @Operation(summary = "发送消息", description = "发送单人消息")
    @ApiResponse(responseCode = "200", description = "Success",
            content = @Content(mediaType = "application/json",
                    schema = @Schema(implementation = Result.class)))
    @Parameters({
            @Parameter(name = "fromUid", description = "发送者用户ID", required = true),
            @Parameter(name = "toUid", description = "接收者用户ID", required = true),
            @Parameter(name = "messageContent", description = "消息内容", required = true)
    })
    @PostMapping("/sendMessage")
    public Result<Long> sendMessage(@RequestParam String fromUid,
                                    @RequestParam String toUid,
                                    @RequestParam String messageContent) {
        Long messageId = wuKongImService.sendMessage(fromUid, toUid, messageContent);
        return Result.success(messageId);
    }

    @ApiOperationSupport(order = 45, author = "Qwen")
    @Operation(summary = "同步会话消息", description = "获取用户会话列表")
    @ApiResponse(responseCode = "200", description = "Success",
            content = @Content(mediaType = "application/json",
                    schema = @Schema(implementation = Result.class)))
    @Parameters({
            @Parameter(name = "uid", description = "用户ID", required = true),
            @Parameter(name = "syncSequence", description = "同步序列号", required = false),
            @Parameter(name = "limit", description = "限制数量", required = false)
    })
    @PostMapping("/syncConversationMessages")
    public Result<List<LocalImChatListVo>> syncConversationMessages(@RequestParam String uid,
                                       @RequestParam(required = false) Long syncSequence,
                                       @RequestParam(required = false) Integer limit) {
        List<LocalImChatListVo> result = wuKongImService.getImChatList(uid, syncSequence, limit);
        return Result.success(result);
    }

    @ApiOperationSupport(order = 50, author = "Qwen")
    @Operation(summary = "用户登录或注册", description = "用户登录或注册，返回用户信息和token")
    @ApiResponse(responseCode = "200", description = "Success",
            content = @Content(mediaType = "application/json",
                    schema = @Schema(implementation = Result.class)))
    @Parameters({
            @Parameter(name = "userId", description = "用户ID", required = true)
    })
    @PostMapping("/loginOrRegister")
    public Result<Object> loginOrRegister(@RequestParam String userId) {
        Object result = wuKongImService.loginOrRegister(userId);
        return Result.success(result);
    }

    @ApiOperationSupport(order = 80, author = "Qwen")
    @Operation(summary = "获取用户登录状态", description = "获取用户登录状态和信息")
    @ApiResponse(responseCode = "200", description = "Success",
            content = @Content(mediaType = "application/json",
                    schema = @Schema(implementation = Result.class)))
    @Parameters({
            @Parameter(name = "userId", description = "用户ID", required = true)
    })
    @GetMapping("/getUserLoginStatus")
    public Result<Object> getUserLoginStatus(@RequestParam String userId) {
        Object loginStatus = wuKongImService.getUserLoginStatus(userId);
        return Result.success(loginStatus);
    }

    @ApiOperationSupport(order = 85, author = "Qwen")
    @Operation(summary = "获取集群会话消息列表", description = "获取用户集群会话列表")
    @ApiResponse(responseCode = "200", description = "Success",
            content = @Content(mediaType = "application/json",
                    schema = @Schema(implementation = Result.class)))
    @Parameters({
            @Parameter(name = "uid", description = "用户ID", required = true)
    })
    @GetMapping("/getClusterConversationMessages")
    public Result<List<LocalImChatListVo>> getClusterConversationMessages(@RequestParam String uid) {
        List<LocalImChatListVo> result = wuKongImService.getImChatList1(uid, null, null);
        return Result.success(result);
    }
}